//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package org_asset_api_serv

import (
	"context"
	"time"

	"atomgit.com/openlinksaas/api-server/dao"
	"atomgit.com/openlinksaas/api-server/dao/org_dao"
	"atomgit.com/openlinksaas/proto-gen-go.git/org_asset_api"
	"github.com/google/uuid"
	"go.mongodb.org/mongo-driver/mongo"
)

func (apiImpl *OrgAssetApiImpl) AddAssetCate(ctx context.Context, req *org_asset_api.AddAssetCateRequest) (*org_asset_api.AddAssetCateResponse, error) {
	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &org_asset_api.AddAssetCateResponse{
			Code:   org_asset_api.AddAssetCateResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	orgItem, err := org_dao.OrgInfoDao.Get(ctx, req.OrgId)
	if err != nil {
		return &org_asset_api.AddAssetCateResponse{
			Code:   org_asset_api.AddAssetCateResponse_CODE_NO_ORG,
			ErrMsg: "组织不存在",
		}, nil
	}
	selfMemberItem, err := org_dao.MemberDao.Get(ctx, req.OrgId, sessionItem.UserId)
	if err != nil {
		return &org_asset_api.AddAssetCateResponse{
			Code:   org_asset_api.AddAssetCateResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	if !(orgItem.OwnerUserId == sessionItem.UserId || selfMemberItem.MemberPermInfo.ManageAssetCate) {
		return &org_asset_api.AddAssetCateResponse{
			Code:   org_asset_api.AddAssetCateResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	nowTime := time.Now().UnixNano() / 1e6
	assetCateId := uuid.NewString()

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = org_dao.AssetCateDao.Insert(sessCtx, &org_dao.AssetCateInfoDbItem{
			AssetCateId: assetCateId,
			OrgId:       req.OrgId,
			CateName:    req.CateName,
			AssetCount:  0,
			TimeStamp:   nowTime,
		})
		if err != nil {
			return nil, err
		}
		return &org_asset_api.AddAssetCateResponse{
			Code:        org_asset_api.AddAssetCateResponse_CODE_OK,
			AssetCateId: assetCateId,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*org_asset_api.AddAssetCateResponse), nil
}

func (apiImpl *OrgAssetApiImpl) ListAssetCate(ctx context.Context, req *org_asset_api.ListAssetCateRequest) (*org_asset_api.ListAssetCateResponse, error) {
	emptyList := []*org_asset_api.AssetCateInfo{}

	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &org_asset_api.ListAssetCateResponse{
			Code:     org_asset_api.ListAssetCateResponse_CODE_WRONG_SESSION,
			ErrMsg:   "会话不存在",
			CateList: emptyList,
		}, nil
	}
	exist, err := org_dao.OrgInfoDao.Exist(ctx, req.OrgId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &org_asset_api.ListAssetCateResponse{
			Code:     org_asset_api.ListAssetCateResponse_CODE_NO_ORG,
			ErrMsg:   "组织不存在",
			CateList: emptyList,
		}, nil
	}
	exist, err = org_dao.MemberDao.Exist(ctx, req.OrgId, sessionItem.UserId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &org_asset_api.ListAssetCateResponse{
			Code:     org_asset_api.ListAssetCateResponse_CODE_NO_PERMISSION,
			ErrMsg:   "没有权限",
			CateList: emptyList,
		}, nil
	}

	cateItemList, err := org_dao.AssetCateDao.List(ctx, req.OrgId)
	if err != nil {
		return nil, err
	}
	cateList := []*org_asset_api.AssetCateInfo{}
	for _, cateItem := range cateItemList {
		cateList = append(cateList, cateItem.ToAssetCateInfo())
	}

	return &org_asset_api.ListAssetCateResponse{
		Code:     org_asset_api.ListAssetCateResponse_CODE_OK,
		CateList: cateList,
	}, nil
}

func (apiImpl *OrgAssetApiImpl) UpdateAssetCate(ctx context.Context, req *org_asset_api.UpdateAssetCateRequest) (*org_asset_api.UpdateAssetCateResponse, error) {
	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &org_asset_api.UpdateAssetCateResponse{
			Code:   org_asset_api.UpdateAssetCateResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	orgItem, err := org_dao.OrgInfoDao.Get(ctx, req.OrgId)
	if err != nil {
		return &org_asset_api.UpdateAssetCateResponse{
			Code:   org_asset_api.UpdateAssetCateResponse_CODE_NO_ORG,
			ErrMsg: "组织不存在",
		}, nil
	}
	selfMemberItem, err := org_dao.MemberDao.Get(ctx, req.OrgId, sessionItem.UserId)
	if err != nil {
		return &org_asset_api.UpdateAssetCateResponse{
			Code:   org_asset_api.UpdateAssetCateResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	if !(orgItem.OwnerUserId == sessionItem.UserId || selfMemberItem.MemberPermInfo.ManageAssetCate) {
		return &org_asset_api.UpdateAssetCateResponse{
			Code:   org_asset_api.UpdateAssetCateResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}
	exist, err := org_dao.AssetCateDao.Exist(ctx, req.OrgId, req.AssetCateId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &org_asset_api.UpdateAssetCateResponse{
			Code:   org_asset_api.UpdateAssetCateResponse_CODE_NO_ASSET_CATE,
			ErrMsg: "资产类别不存在",
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = org_dao.AssetCateDao.UpdateCateName(sessCtx, req.OrgId, req.AssetCateId, req.CateName)
		if err != nil {
			return nil, err
		}
		return &org_asset_api.UpdateAssetCateResponse{
			Code: org_asset_api.UpdateAssetCateResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*org_asset_api.UpdateAssetCateResponse), nil
}

func (apiImpl *OrgAssetApiImpl) RemoveAssetCate(ctx context.Context, req *org_asset_api.RemoveAssetCateRequest) (*org_asset_api.RemoveAssetCateResponse, error) {
	sessionItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &org_asset_api.RemoveAssetCateResponse{
			Code:   org_asset_api.RemoveAssetCateResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	orgItem, err := org_dao.OrgInfoDao.Get(ctx, req.OrgId)
	if err != nil {
		return &org_asset_api.RemoveAssetCateResponse{
			Code:   org_asset_api.RemoveAssetCateResponse_CODE_NO_ORG,
			ErrMsg: "组织不存在",
		}, nil
	}
	selfMemberItem, err := org_dao.MemberDao.Get(ctx, req.OrgId, sessionItem.UserId)
	if err != nil {
		return &org_asset_api.RemoveAssetCateResponse{
			Code:   org_asset_api.RemoveAssetCateResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	if !(orgItem.OwnerUserId == sessionItem.UserId || selfMemberItem.MemberPermInfo.ManageAssetCate) {
		return &org_asset_api.RemoveAssetCateResponse{
			Code:   org_asset_api.RemoveAssetCateResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}
	cateItem, err := org_dao.AssetCateDao.Get(ctx, req.OrgId, req.AssetCateId)
	if err != nil { //资产类别不存在
		return &org_asset_api.RemoveAssetCateResponse{
			Code: org_asset_api.RemoveAssetCateResponse_CODE_OK,
		}, nil
	}
	if cateItem.AssetCount > 0 {
		return &org_asset_api.RemoveAssetCateResponse{
			Code:   org_asset_api.RemoveAssetCateResponse_CODE_HAS_ASSET,
			ErrMsg: "存在关联资产",
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = org_dao.AssetCateDao.Remove(sessCtx, req.OrgId, req.AssetCateId)
		if err != nil {
			return nil, err
		}
		return &org_asset_api.RemoveAssetCateResponse{
			Code: org_asset_api.RemoveAssetCateResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*org_asset_api.RemoveAssetCateResponse), nil
}
